package pers.vinsonws.jianghu.serverspring.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vinsonws.jianghu.serverspring.entity.ChatRooms;
import pers.vinsonws.jianghu.serverspring.entity.Users;
import pers.vinsonws.jianghu.serverspring.repositories.ChatRoomsRepository;
import pers.vinsonws.jianghu.serverspring.repositories.UsersRepository;
import pers.vinsonws.jianghu.serverspring.tools.IdGenerator;
import pers.vinsonws.jianghu.serverspring.tools.Transition;
import pers.vinsonws.jianghu.util.User;

import javax.annotation.PostConstruct;


/**
 * @auther vinsonws
 * @date 2018/11/5 17:10
 */
@Service
public class UserServiceImpl implements UserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UsersRepository usersRepository;
    @Autowired
    private ChatRoomsRepository chatRoomsRepository;

    @Override
    public User loginByUidAndPassword(String uid, String password) {
        log.info("验证用户名密码中......");
        Users users =  usersRepository.findByUid(uid);
        if (!users.getPassword().equals(password)){
            log.error("uid为"+uid+"密码错误！");
            return null;
        }
        log.info(users.toString());
        return Transition.fromUsersToUser(users, new User());
    }

    @Override
    public String registerByUserNameAndPassword(String userName, String password) throws Exception {
        log.info("注册用户中......");
        Users users = new Users();
        users.setUserName(userName);
        users.setPassword(password);
        users.setAuthorityCode(1);
        users.setUid(IdGenerator.getUid());
        users = usersRepository.saveAndFlush(users);
        if (users != null){
            log.info("注册成功！Uid为，" + users.getUid() + "用户名为" + users.getUserName() + "。");
            return users.getUid();
        } else {
            log.info("注册用户名为" + userName + "失败！");
            throw new Exception("注册用户名为" + userName + "失败！");
        }
    }

    @Override
    public boolean updateUserInformation(User user, String userName, String oldPassword, String newPassword) {
        log.info("修改用户信息......");
        if (!oldPassword.equals(user.getPassword())) {
            log.info("密码错误");
            return false;
        }
        Users users = usersRepository.findById(user.getId());
        users.setUserName(userName);
        users.setPassword(newPassword);
        usersRepository.saveAndFlush(users);
        return true;
    }

    @Override
    public User getUserById(int id) {
        return Transition.fromUsersToUser(usersRepository.findById(id), new User());
    }

    @Override
    public void addChatRoomByUserId(int userId, ChatRooms chatRooms) {
        Users users = usersRepository.findById(userId);
        users.getChatRooms().add(chatRooms);
        usersRepository.saveAndFlush(users);
    }


    //测试
    @PostConstruct
    private void asdasadas(){
        System.out.println("UserServiceImpl使用！");
    }
}
